/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.customer;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.customer.Restatus;
import src.services.customer.RestatusModel;
import src.util.Messager;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class RestatusBean implements Serializable {

    private List<Restatus> restatuss;
    private Restatus selectedData, insertedData;
    private boolean editMode = true;
    private String filter;

    public RestatusBean() {
        RestatusModel helper = new RestatusModel();
        this.restatuss = helper.findAll(null);
        this.insertedData = null;
        this.selectedData = null;
    }

    public boolean isEditMode() {
        return editMode;
    }
    
    public List<Restatus> getRestatuss() {
        return this.restatuss;
    }
    
    public Restatus getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Restatus selectedData) {
        this.selectedData = selectedData;
    }
    
    public Restatus getInsertedData() {
        return insertedData;
    }

    public void setInsertedData(Restatus insertedData) {
        this.insertedData = insertedData;
    }
    
    public String getFilter() {
        return filter;
    }

    public void setFilter(String filter) {
        this.filter = filter;
    }
    
    public void search() {
        Map<String, String> filters = new HashMap<>();
      
        filters.put("restatusName", getFilter());
        RestatusModel helper = new RestatusModel();      
        this.restatuss = helper.findAll(filters);
        setFilter("");
    }
        
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.insertedData = null;
        this.editMode = true;      
    }
    
    public void newData() {  
        this.selectedData = null;
        this.editMode = false;
        this.insertedData = new Restatus();
    }
    
     public void openData() {  
        this.editMode = true;
        this.insertedData = Restatus.clone(selectedData);
    }
    
    public void save() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
         RestatusModel helper = new RestatusModel();
        boolean success = helper.saveData(insertedData);
        if (success==true) {
            if (this.editMode==false) this.restatuss.add(insertedData);
            else {
                int i = restatuss.indexOf(selectedData);
                if (i <= restatuss.size()) restatuss.set(i, Restatus.clone(insertedData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) insertedData.setRestatusId(null);
        } 
        
        /*boolean success;
             
        Restatus data = this.insertedData;
        if (this.insertedData == null) data = this.selectedData;
        RestatusModel helper = new RestatusModel();
        success = helper.saveData(data);
        if (success && this.editMode!=true) {
            this.restatuss.add(this.insertedData);
        }
          
          * 
          */
        if(!success) Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success;  
        RestatusModel helper = new RestatusModel();        
        success = helper.deleteData(this.selectedData);
        
        if (success) {
            this.restatuss.remove(this.selectedData);
            this.selectedData = null;
        }
               
        if(!success) Messager.showDeleteStatusConstaint(success);
        context.addCallbackParam("success", success);  
    }
    
}
